# Ultrasonic Sensor

# Obstacle Detection and Navigation

Our DC motor robot also has an ultrasonic sensor for detecting obstacles. This will allow us to use feedback for navigation logic. The HC-SR04 ultrasonic sensor works by sending out a sound wave and measuring how long it takes to return.

The program below triggers the ultrasonic sensor by sending a HIGH signal for 10 microseconds and then uses pulseIn to measure the echo duration in microseconds. The Serial output shows this duration, which can be converted to distance.

Exercise

Click the buttons under the robot environment to change the robot's position. Find the distance of the obstacle for each position.